# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

from conan import ConanFile
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.files import get, copy, symlinks, chdir
from conan.tools.layout import basic_layout
from conan.tools.env import Environment, VirtualBuildEnv
import os

requiled_conan_version = ">=2.13.0"


class BusyboxConan(ConanFile):
    name = "busybox"
    description = "The Swiss Army Knife of Embedded Linux"
    license = "GPL-2.0-or-later"
    homepage = "https://busybox.net/"
    url = "https://github.com/conan-io/conan-center-index"
    settings = "os", "arch", "compiler", "build_type"
    exports_sources = "permissions.ini", "services/telnetd.service"

    options = {
        "shared": [True, False],
        "fPIC": [True, False]
    }
    default_options = {
        "shared": False,
        "fPIC": True
    }

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def layout(self):
        basic_layout(self)

    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)

    def generate(self):
        # 生成 Autotools 工具链
        tc = AutotoolsToolchain(self)
        env = tc.environment()
        chost = self.buildenv.vars(self).get("CHOST")
        if chost:
            self.output.info(f"Setting CROSS_COMPILE={chost}-")
            env.define("CROSS_COMPILE", f"{chost}-")
        tc.generate(env)

    def build(self):
        with chdir(self, self.source_folder):
            # 跳过 x86_64 架构的构建（保留原始逻辑）
            if self.settings.arch == "x86_64":
                self.output.warning("Skipping build for x86_64 architecture")
                return
            
            autotools = Autotools(self)
            # 执行 defconfig 和构建
            autotools.make(target="defconfig")
            autotools.make()

    def package(self):
        # 跳过 x86_64 架构的打包
        if self.settings.arch == "x86_64":
            self.output.warning("Skipping package for x86_64 architecture")
            return
        
        # 复制 busybox 可执行文件
        copy(self, "busybox", 
             src=self.source_folder, 
             dst=os.path.join(self.package_folder, "opt", "debug"))
        
        # 复制权限配置文件
        copy(self, "permissions.ini", 
             src=self.source_folder, 
             dst=self.package_folder)
        
        # 复制 systemd 服务文件
        copy(self, "*.service", 
             src=os.path.join(self.source_folder, "services"), 
             dst=os.path.join(self.package_folder, "lib", "systemd", "system"))
        
        # 创建 systemd 服务链接
        with chdir(self, self.package_folder):
            os.makedirs("etc/systemd/system/multi-user.target.wants")
            self.run("ln -sf ../../../../lib/systemd/system/telnetd.service etc/systemd/system/multi-user.target.wants/telnetd.service")